import { DeptModel, AddDeptModel, ListParam, DeptListResult } from '@/api/dept/DeptModel'
import { ref } from 'vue'
import addAndEdit from '@/views/system/department/addAndEdit.vue'
import { EditType } from '@/type/BaseEnum'
import userInstance from '@/hooks/userInstance'
import { ElMessage } from 'element-plus'
import { addDeptApi, editDeptApi, deleteDeptApi } from '@/api/dept/dept'
import { Result, StatusCode } from '@/http/request'


export default function userDept(getDeptList,searchParams:ListParam) {
    const { global, proxy } = userInstance()

    // 引用新增或编辑的组件 <addAndEditVue ref="addDeptRef"></addAndEditVue>
    const addDeptRef = ref<{show:(type: string, row?: DeptModel) => void}>()

    // 这种方式在打包的时候会出错
    // const addDeptRef = ref<InstanceType<typeof addAndEdit>>()

    // 搜索
    const searchBtn = () => {
        getDeptList()
    }
    // 新增
    const addBtn = () => {
        addDeptRef.value?.show(EditType.ADD)
    }
    // 重置
    const resetBtn = async () => {
        searchParams.searchName = ''
        getDeptList()
    }
    // 编辑
    const editBtn = (row: DeptModel) => {
        addDeptRef.value?.show(EditType.EDIT, row)
    }
    // 删除
    const deleteBtn = async (id: number) => {
        // console.log(global)
        // console.log(proxy)
        let params = { id: id }
        const confirm = await global.$myConfirm('确定删除该数据吗?')
        console.log(confirm)
        if(confirm) {
            // 执行删除的操作
            let res = await deleteDeptApi(params)
            // console.log(res)
            if(res && res.code === StatusCode.Success) {
                global.$message({
                    message: res.msg,
                    type: 'success'
                })
                getDeptList()
            }
        }

        // 原本的对话框
        // global.$confirm(
        //     'proxy will permanently delete the file. Continue?',
        //     'Warning',
        //     {
        //       confirmButtonText: 'OK',
        //       cancelButtonText: 'Cancel',
        //       type: 'warning',
        //     }
        // ).then(() => {
        //     ElMessage({
        //       type: 'success',
        //       message: 'Delete completed',
        //     })
        //   }).catch(() => {
        //     ElMessage({
        //       type: 'info',
        //       message: 'Delete canceled',
        //     })
        // })
    }
    // 保存（更新，新增）
    const saveBtn = async (params: AddDeptModel) => {
        console.log('父组件取到值了,保存===')
        console.log(params)
        // type: 0新增  1编辑
        let res: Result
        if(params.type === EditType.ADD) {
            res = await addDeptApi(params)
        } else {
            res = await editDeptApi(params)
        }
        if(res && res.code === StatusCode.Success) {
            global.$message({
                message: res.msg,
                type: 'success'
            })
            // 刷新表格
            getDeptList()
        }
    }

    return {
        searchBtn,
        addBtn,
        resetBtn,
        editBtn,
        deleteBtn,
        saveBtn,
        addDeptRef
    }
}